<template>
  <div :class="className" :style="{height:height,width:width}"></div>
</template>

<script>
  import echarts from 'echarts'
  require('echarts/theme/dark') // echarts theme
  import resize from './resize'
  // import { req } from '../../../../api/base/BaseApi'

  export default {
    mixins: [resize],
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '400px'
      }
    },
    data() {
      return {
        chart: null,
        data: []
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.initChart()
        setInterval(() => {
          this.updateChart()
        }, 2000)
      })
    },
    beforeDestroy() {
      this.chart.clear()
    },
    methods: {
      initChart() {
        this.chart = echarts.init(this.$el, 'dark')
        this.chart.setOption({
          backgroundColor: '#3A4057',
          tooltip: {
            formatter: '{a} <br/>{b} : {c}%'
          },
          series: [
            {
              name: '倾斜测点1',
              type: 'gauge',
              min: -15,
              max: 15,
              detail: { formatter: '{value}°' },
              data: [{ value: 5, name: '测点1' }]
            }
          ]
        })
      },
      updateChart() {
        this.data = [{
          value: (Math.random() * 100).toFixed(2) - 0,
          name: '测点1' }]
        this.chart.setOption({
          series: [
            {
              data: this.data
            }
          ]
        })
      }
    }
  }
</script>

<style scoped>

</style>
